
//引入全局vue示例
import vue from "../main";
let _this;
setTimeout(() => {
  _this = vue;
})

const lcM = {

  /* 参数同步 */
  saveSearchF({ _this, subItemName }) {
    var searchP = _this.tools.getSeObj("searchObj") || {};
    var curSubP = {};
    curSubP = _this.formData;
    curSubP.currentPageIdx = _this.currentPageIdx;
    curSubP.onePageSize = _this.onePageSize;
    searchP[subItemName] = curSubP;
    _this.tools.setSeObj("searchObj", searchP);
  },

   /* 恢复搜索状态 */
   recoverSearchF({ _this, subItemName }) {
    var searchP = _this.tools.getSeObj("searchObj") || {};
    var curSubP = searchP[subItemName];
    if (!curSubP) return;
    for (var itemKey in curSubP) {
      _this.formData[itemKey] = curSubP[itemKey];
    }
    _this.currentPageIdx = _this.formData.currentPageIdx;
    _this.onePageSize = _this.formData.onePageSize;
  },


  /* 搜索参数同步 */
  searchParamsSync({ _this }) {
    var query = _this.$route.query;
    for (var itemKey in query) {
      _this.formData[itemKey] = query[itemKey];
    }
  },


  /* 输入弹框 */
  inputModelF({
    _this,
    content = "请输入",
    title = "提示",
    btnTips = "确定",
    success = () => { },
    fail = () => { } }) {
    _this.$prompt(content, title, {
      confirmButtonText: btnTips,
      cancelButtonText: '取消',
      // inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
      // inputErrorMessage: '邮箱格式不正确'
    }).then(({ value }) => {

      success(value)

    }).catch(() => {

      // this.$message({
      //   type: 'info',
      //   message: '取消输入'
      // });

    });

    /* 
      this.tools.inputModelF({
        _this: this,
        content: "请输入邮箱",
        success: value => {
          console.log(value);
        }
      })
    
    */
  },

  /* 确认弹框 */
  comfirmF({
    _this,
    content = "内容",
    title = "提示",
    btnTips = "确认",
    success = () => { },
    fail = () => { }
  }) {

    _this.$confirm(content, title, {
      confirmButtonText: btnTips,
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      success();
    }).catch(() => {
      fail();
    });

    /* 
       this.tools.comfirmF({
        _this: this,
        content: "是否确认此操作？",
        success: () => {
          
        }
      });
    
    */
  },

  /* 提示信息 */
  Model({ msg, title = "提示信息", btnText = "确定", success = () => { }, }) {
    title = msg || title;
    _this.$alert(title, {
      confirmButtonText: btnText,
      callback: (action) => {
        success(action);
      },
    });
    /* 
      this.tools.Model({
        msg: "提示",
      })
    */
  },

  /* 触发全局自定义事件 */
  touchGEvent({ eName, e, imgUrl }) {
    // // 创建自定义事件 blankChart
    // var event = document.createEvent("HTMLEvents");
    // // 初始化testEvent事件

    // event.initEvent("previewImg", false, true);
    // // event.initEvent(eName, false, true);

    // // 触发自定义事件
    // window.dispatchEvent(event, 123);


    var event = new CustomEvent("previewImg", {
      "detail": {
        "hazcheeseburger": true,
        imgUrl: eName == "previewImg" ? imgUrl : "",
      }
    });
    window.dispatchEvent(event);

    /* 
     //触发图片点击事件
      this.tools.touchGEvent({
        eName: "previewImg",
      });
    */
  },

  /* 监听全局自定义事件 */
  listenGEvent({ eName, success }) {
    window.addEventListener(eName, function (res) {
      success(res);
    });
    /* 
     //初始化图片预览事件
      this.tools.listenGEvent({
        eName: "previewImg",
        success: (res) => {
          console.log(res);
        },
      });
    */
  },

  // 检查电话格式
  checkPhoneNum(phone) {
    phone = phone + '';
    var isPass = true;
    isPass = /^1[3456789]\d{9}$/.test(phone);
    if (!isPass) {
      _this.tools.Model({
        title: "手机号格式不正确",
      });
    }
    return isPass;
  },

  /* 倒计时 */
  countDownF({ elName, timeN, tagN, _this }) {
    var curCount = timeN;
    _this[tagN] = false;
    var timer = setInterval(() => {
      curCount--;
      var str = curCount + "秒后重新获取";
      $(elName).html(str);
      if (curCount == 0) {
        $(elName).html("获取验证码");
        _this[tagN] = true;
        clearInterval(timer);
      }
    }, 999);
  },

}

export default lcM;